package com.citywy.service;

import com.citywy.dao.StudentDao;
import com.citywy.domain.Student;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class StudentDaoImpl implements StudentDao {
    private SqlSessionFactory factory;

    public StudentDaoImpl(SqlSessionFactory factory) {
        this.factory = factory;
    }

    @Override
    public List<Student> findAll() {
        //根据factory获取sqlsession对象
        SqlSession sqlSession = factory.openSession();
        List<Student> studentList = sqlSession.selectList("com.citywy.dao.StudentDao.findAll");
        sqlSession.close();
        return studentList;
    }

    @Override
    public void insert(Student student) {
        SqlSession sqlSession = factory.openSession();
        int a = sqlSession.insert("com.citywy.dao.StudentDao.insert",student);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void delete(Integer id) {
        SqlSession session = factory.openSession();
        int a = session.delete("com.citywy.dao.StudentDao.delete",id);
        session.commit();
        session.close();
    }

    @Override
    public void update(Student student) {
        SqlSession session = factory.openSession();
        int a = session.update("com.citywy.dao.StudentDao.update",student);
        session.commit();
        session.close();
    }

    @Override
    public Student getById(Integer id) {
        SqlSession session = factory.openSession();
        Student student = session.selectOne("com.citywy.dao.StudentDao.getById",id);
        session.close();
        return student;
    }
}
